<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

//购物车控制器
class Cart extends Home_Controller{

	public function __construct(){
		parent::__construct();
        $this->load->model('goods_model');

	}
    #显示购物车页面
    public function show(){
        #获取购物车数据
        $carts = $this->cart->contents();
        foreach ($carts as $k=>$v){
            $goods_info = $this->goods_model->get_goods($v['id']);
            $goods_img = $goods_info['goods_img'];
            $imgs = explode(',',$goods_img);
            $good_imgfile = pathinfo($imgs[0]);
            $filename =$good_imgfile["filename"].'_thumb'.'.';
            $img_name =$filename.$good_imgfile["extension"];
            $carts[$k]['img_name'] = $img_name;
        }

        $data['carts'] = $carts;
        $this->load->view('flow.html',$data);
    }


	public function join()
    {

        //看session是否存在当前用户user_id，不存在就让其登录
        //当前用户user_id
        $user=$this->session->userdata('user');
        if(!empty($user)){
            //说明已经登录
            $data['id'] = $this->input->post('goods_id');
            $data['name'] = $this->input->post('goods_name');
            $data['qty'] = $this->input->post('goods_nums');
            $data['price'] = $this->input->post('shop_price');


            #在插入之前，需要判断即将要加入的商品是否已经存在于购物车中
            $carts = $this->cart->contents();
            foreach ($carts as $v) {
                if ($v['id'] == $data['id']){
                    $data['qty'] += $v['qty'];
                }
            }

            if ($this->cart->insert($data)) {
                //加入购物车成功
                $mess = array();
                $mess['stat'] = 1;
                $mess['message'] = '加入购物车成功';
                $mess['url'] = base_url().'cart/show';
                echo json_encode($mess);
                exit();
            } else {
                //加入购物车失败
                $mess = array();
                $mess['stat'] = 0;
                $mess['message'] = '加入购物车失败';
                //$mess['url'] = base_url().'cart/show';
                echo json_encode($mess);
                exit();
            }
            var_dump($data);

        }else{
            //说明没有登录，告知登录去
            $mess = array();
            $mess['stat'] = 0;
            $mess['message'] = '请先登录';
            $mess['url'] = base_url().'user/login';
            echo json_encode($mess);
            exit();
        }
    }

    #删除购物车信息
    public function delete(){
        $rowid = $this->input->get('rowid');
        $data['rowid'] = $rowid;
        $data['qty'] = 0;
        if($this->cart->update($data)){
            //删除购物车成功
            $mess = array();
            $mess['stat'] = 1;
            $mess['message'] = '删除成功';
            $mess['url'] = base_url().'cart/show';
            echo json_encode($mess);
            exit();
        }else{
            //删除购物车失败
            $mess = array();
            $mess['stat'] = 0;
            $mess['message'] = '删除失败';
            $mess['url'] = base_url().'cart/show';
            echo json_encode($mess);
            exit();
        }

    }


}